iT邦幫忙

2022 iThome 鐵人賽

DAY 4
0
自我挑戰組

探索Django及Flask的世界系列 第 4

Day04:讓Django動起來!

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20220917/20128286GEJgYtV4W1.jpg

今天開始進入正題,一如往常,專案的運行少不了貓貓的陪伴,紀念我家貓咪交到貓咪朋友的瞬間。

前一篇提到設置虛擬環境,今天就讓Django開始啟動,網路上有很多Django的教學資源,有不錯的資源也會貼上來與各位分享,希望各位不吝指教。
開始啟動專案,前幾天有提到,環境建置完成後,到終端機輸入下列指令開始專案,若是遇到錯誤,可以參考官網-Troubleshooting

  • 啟動專案
# mysite是你的專案名稱
$ django-admin startproject mysite

成功啟動專案後,終端機不會顯示任何狀態,可以cd到你剛剛建立的專案文件下,查看當前文件名。

可以使用tree -aCDFt這個指令查看當前檔案夾下的所有文件,會以樹狀圖的方式呈現,方便閱讀。

(base) XXX@MacBook-Air mysite % tree -aCDFt
.
├── [Sep 17 09:33]  manage.py*
└── [Sep 17 09:33]  mysite/
    ├── [Sep 17 09:33]  __init__.py
    ├── [Sep 17 09:33]  asgi.py
    ├── [Sep 17 09:33]  settings.py
    ├── [Sep 17 09:33]  urls.py
    └── [Sep 17 09:33]  wsgi.py

1 directory, 6 files

創建完成後,可以透過下列指令確認是否有創建成功:

$ python manage.py runserver

創建成功的畫面在終端機會顯示如下:

(base) emma@MacBook-Air mysite % python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
Run 'python manage.py migrate' to apply them.
September 17, 2022 - 01:42:20
Django version 3.1.7, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

啟動了Django專案後,終端機有出現一行本地端網址要輸入瀏覽器,其中後面的8000是端口號的意思,我們可以更改端口號,例如:

$ python manage.py runserver 8080

啟動這個命令,端口號就改為8080。

若是要退出運行模式,只要依照提示同時按住CONTROL與C即可退出。在終端機啟動成功後,接下來在瀏覽器輸入上述網址,在本地端查看是否有運行成功。
https://ithelp.ithome.com.tw/upload/images/20220917/20128286n72UVXazvv.png

畫面中可以同時看到終端機的狀態碼是200,代表資料運送成功,右圖畫面也顯示Django經典畫面,恭喜你!啟動了你的第一個Django專案。


文章中提到的「在終端機列印出樹狀圖」這個功能很方便,首先先安裝homebrew,在使用homebrew的指令安裝tree:

$ brew install tree

安裝完成後,直接在終端機輸入tree這個指令就可以顯示樹狀圖,當然他也有其他參數,提供你需要的幫助:

$ tree

(base) xxx@MacBook-Air mysite % tree
.
├── db.sqlite3
├── manage.py
└── mysite
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-37.pyc
    │   ├── settings.cpython-37.pyc
    │   ├── urls.cpython-37.pyc
    │   └── wsgi.cpython-37.pyc
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

2 directories, 11 files

在終端機輸入tree --help,就會列出tree可以使用的所有參數:

$ tree --help

usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-H baseHREF] [-T title ]
        [-L level [-R]] [-P pattern] [-I pattern] [-o filename] [--version]
        [--help] [--inodes] [--device] [--noreport] [--nolinks] [--dirsfirst]
        [--charset charset] [--filelimit[=]#] [--si] [--timefmt[=]<f>]
        [--sort[=]<name>] [--matchdirs] [--ignore-case] [--fromfile] [--]
        [<directory list>]
  ------- Listing options -------
  -a            All files are listed.
  -d            List directories only.
  -l            Follow symbolic links like directories.
  -f            Print the full path prefix for each file.
  -x            Stay on current filesystem only.
  -L level      Descend only level directories deep.
  -R            Rerun tree when max dir level reached.
  -P pattern    List only those files that match the pattern given.
  -I pattern    Do not list files that match the given pattern.
  --ignore-case Ignore case when pattern matching.
  --matchdirs   Include directory names in -P pattern matching.
  --noreport    Turn off file/directory count at end of tree listing.
  --charset X   Use charset X for terminal/HTML and indentation line output.
  --filelimit # Do not descend dirs with more than # files in them.
  --timefmt <f> Print and format time according to the format <f>.
  -o filename   Output to file instead of stdout.
  ------- File options -------
  -q            Print non-printable characters as '?'.
  -N            Print non-printable characters as is.
  -Q            Quote filenames with double quotes.
  -p            Print the protections for each file.
  -u            Displays file owner or UID number.
  -g            Displays file group owner or GID number.
  -s            Print the size in bytes of each file.
  -h            Print the size in a more human readable way.
  --si          Like -h, but use in SI units (powers of 1000).
  -D            Print the date of last modification or (-c) status change.
  -F            Appends '/', '=', '*', '@', '|' or '>' as per ls -F.
  --inodes      Print inode number of each file.
  --device      Print device ID number to which each file belongs.
  ------- Sorting options -------
  -v            Sort files alphanumerically by version.
  -t            Sort files by last modification time.
  -c            Sort files by last status change time.
  -U            Leave files unsorted.
  -r            Reverse the order of the sort.
  --dirsfirst   List directories before files (-U disables).
  --sort X      Select sort: name,version,size,mtime,ctime.
  ------- Graphics options -------
  -i            Don't print indentation lines.
  -A            Print ANSI lines graphic indentation lines.
  -S            Print with CP437 (console) graphics indentation lines.
  -n            Turn colorization off always (-C overrides).
  -C            Turn colorization on always.
  ------- XML/HTML/JSON options -------
  -X            Prints out an XML representation of the tree.
  -J            Prints out an JSON representation of the tree.
  -H baseHREF   Prints out HTML format with baseHREF as top directory.
  -T string     Replace the default HTML title and H1 header with string.
  --nolinks     Turn off hyperlinks in HTML output.
  ------- Input options -------
  --fromfile    Reads paths from files (.=stdin)
  ------- Miscellaneous options -------
  --version     Print version and exit.
  --help        Print usage and this help message and exit.
  --            Options processing terminator.

一個方便的小工具,提供給學習者參考!明天見!


上一篇
Day03:開始之前
系列文
探索Django及Flask的世界4
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言